/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_REFLECTION_TOOLS_HPP1
#define	_CUBEFS_CORE_API_REFLECTION_TOOLS_HPP1

#include <string>
#include <sstream>
#include <cxxabi.h>
#include <typeinfo>

#include <cubefs/api/types.hpp>

namespace cubefs    {
namespace api       {

template<typename T, Int Size = 4096> struct TypeNameFactory;

//template<typename T, int BufferSize = 4096>
//struct TypeNameFactory
//{
//    static std::string name() {
//        char buf[BufferSize];
//        size_t len = sizeof(buf);
//        abi::__cxa_demangle(typeid(T).name(), buf, &len, NULL);
//        return std::string(buf);
//    }
//};


#define CUBEFS_DECLARE_TYPENAME(T, Name) 					\
template<Int Size>                          				\
struct TypeNameFactory<T, Size>          					\
{                                           				\
    static String name() {             						\
        return String(Name);           						\
    }                                       				\
}

#define CUBEFS_DECLARE_PARAMETRIZED_TYPENAME(TypeName, Name)	\
template<typename T, Int Size>                          		\
struct TypeNameFactory<TypeName<T>, Size>          				\
{                                           					\
    static String name() {             							\
        return String(String(Name)+"<"+TypeNameFactory<T>::name()+">");	\
    }                                       					\
}

#define CUBEFS_DECLARE_PARAMETRIZED_TYPENAME1(TypeName, Name)	\
template<typename T, Int Size, Int Size0>                       \
struct TypeNameFactory<TypeName<T, Size>, Size0>          			\
{                                           					\
    static String name() {             							\
		std::stringstream str;									\
		str<<Size;												\
        return String(String(Name)+"<"+TypeNameFactory<T>::name()+","+str.str()+">");\
    }                                       					\
}

template<typename T, Int Size>
struct TypeNameFactory<T*, Size>
{
    static String name() {
        return String(TypeNameFactory<T>::name()+"*");
    }
};

template<typename T, Int Size>
struct TypeNameFactory<const T, Size>
{
    static String name() {
        return String("const "+TypeNameFactory<T>::name());
    }
};

template<typename T, Int Size>
struct TypeNameFactory<T&, Size>
{
    static String name() {
        return String(TypeNameFactory<T>::name()+"&");
    }
};


}
}

#endif
